package com.yunjian.core.basic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunjian.core.basic.mapper.SUserMapper;
import com.yunjian.core.basic.model.SUser;
import com.yunjian.core.basic.model.SUserExample;
import com.yunjian.core.basic.service.SUserService;

@Service
public class SUserServiceImpl implements SUserService{
	
	//private final static Logger logger = LoggerFactory.getLogger(SUserServiceImpl.class);
	
	@Autowired
	private SUserMapper mapper;

	@Override
	public int countByExample(SUserExample example) {
		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(SUserExample example) {
		return mapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String userId) {
		return mapper.deleteByPrimaryKey(userId);
	}

	@Override
	public int insert(SUser record) {
		return mapper.insert(record);
	}

	@Override
	public int insertSelective(SUser record) {
		return mapper.insertSelective(record);
	}

	@Override
	public List<SUser> selectByExample(SUserExample example) {
		return mapper.selectByExample(example);
	}

	@Override
	public SUser selectByPrimaryKey(String userId) {
		return mapper.selectByPrimaryKey(userId);
	}

	@Override
	public int updateByExampleSelective(SUser record, SUserExample example) {
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(SUser record, SUserExample example) {
		return mapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(SUser record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(SUser record) {
		return mapper.updateByPrimaryKey(record);
	}

	@Override
	public SUser selectByUserMultiName(String username) {
		if (username == null) {
			return null;
		}
		SUserExample mUserEp = new  SUserExample();
		SUserExample.Criteria sUserCr = mUserEp .createCriteria();
		if (username.matches("^[1][0-9]{10}$")) {
			sUserCr.andUserTelEqualTo(username);
		} else if (username.matches("^([a-z0-9A-Z]+[-|\\._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$")) {
			sUserCr.andUserMailEqualTo(username);
		} else {
			sUserCr.andUserAccountEqualTo(username);
		}
		List<SUser> sUsers = mapper.selectByExample(mUserEp);
		if(sUsers == null || sUsers.isEmpty()){
			return null;
		}
		return sUsers.get(0);
	}

	@Override
	public PageInfo<SUser> selectPagedByExample(int page, int rows,
			SUserExample example) {
		PageHelper.startPage(page, rows);
		PageInfo<SUser> pageInfo = new PageInfo<>(mapper.selectByExample(example));
		return pageInfo;
	}

}
